package cn.tangsihai.user.service.impl;

import cn.tangsihai.basic.exception.MyException;
import cn.tangsihai.basic.service.impl.BasicServiceImpl;
import cn.tangsihai.basic.util.MD5Utils;
import cn.tangsihai.basic.util.StrUtils;
import cn.tangsihai.user.constant.UserConstant;
import cn.tangsihai.user.domain.Logininfo;
import cn.tangsihai.user.domain.User;
import cn.tangsihai.user.dto.UserDto;
import cn.tangsihai.user.mapper.LogininfoMapper;
import cn.tangsihai.user.mapper.UserMapper;
import cn.tangsihai.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PutMapping;

/**
 * 手机号注册业务类
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS) // 事务
public class UserServiceImpl extends BasicServiceImpl<User> implements IUserService {
   @Autowired
    private UserMapper userMapper;
   @Autowired
   private RedisTemplate redisTemplate;
   @Autowired
   private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void phoneRegister(UserDto userDto) {
        // 校验
        // 1.校验非空，四个
        if (StringUtils.isEmpty(userDto.getPhone())
                || StringUtils.isEmpty(userDto.getCode())
                || StringUtils.isEmpty(userDto.getPassword())
                || StringUtils.isEmpty(userDto.getPassword2())){
            throw new MyException("必填项不能为空");
        }
        // 2.校验手机号是否被注册，即user表中是否存在手机号
        User alreadyUser = userMapper.findByPhone(userDto.getPhone());
        if (alreadyUser != null){
            throw new MyException("手机号以被注册，请直接登录");
        }
        // 3.校验验证码是否正确
        Object obj = redisTemplate.opsForValue().get(UserConstant.REGISTER_PREFIX + userDto.getPhone());
        if (obj == null){ // 不存在验证码
            throw new MyException("验证码以失效，请重新获取");
        } else { // 验证码存在，判断输入验证码是否与发送的验证码相同
            String registe = obj.toString().split(":")[1];
            // equalsIgnoreCase：判断字符串是否相同，不区分大小写
            if (!userDto.getCode().equalsIgnoreCase(registe)){
                throw new MyException("验证码不正确");
            }
        }
        // 4.判断两次密码是否一致
        if (!userDto.getPassword().equals(userDto.getPassword2())){
            throw new MyException("两次密码不一致！");
        }

        // 准备数据
        // 1.将前端传来的参数添加到user中
        User user = userDto2user(userDto);
        // 2.将前端传来的参数添加到logininfo中
        Logininfo logininfo = userDto2logininfo(userDto);
        // 添加数据，先添加logininfo，为了获取自增长id
        logininfoMapper.add(logininfo); // 获取到了自增长id
        // 将logininfo中的自增长id设置到user中的logininfo_id中
        user.setLogininfo_id(logininfo.getId());
        // 添加数据到user表中
        userMapper.add(user);

    }

    // 准备数据，将user中的数据复制到logininfo中
    public Logininfo userDto2logininfo(UserDto userDto) {
        Logininfo logininfo = new Logininfo();
        User user = userDto2user(userDto);
        // 将user中的数据复制到logininfo中， BeanUtils.copyProperties：有相同的字段就复制
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1); // type：0 后台人员，1 普通用户
        logininfo.setDisable(1);
        return logininfo;
    }

    // 准备数据，将前端传来的数据添加到user中
    public User userDto2user(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        user.setState(0);
        // 获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        String password = MD5Utils.encrypByMd5(userDto.getPassword() + salt); // 加密加盐
        user.setSalt(salt);
        user.setPassword(password);
        return user;
    }
}
